<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Task;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Task\Ctl\TaskCtl;
use Srv\Apps\Proto\DTO\Task\TaskEditDTO;
use Srv\Apps\Proto\DTO\Task\TaskListDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Task\TaskListVO;
use Srv\Libs\Common\CommJson;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class TaskAct extends ActionAbstract
{
    /**
     * @url /task/list
     * @auth getTaskList
     * @req Srv.Apps.Proto.DTO.Task.TaskListDTO
     * @rsp Srv.Apps.Proto.VO.Task.TaskListVO
     * 任务列表
     */
    public function getTaskList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof TaskListDTO) || !($VOClass instanceof TaskListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = TaskCtl::tag()->getTaskList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /task/edit
     * @auth taskEdit
     * @req Srv.Apps.Proto.DTO.Task.TaskEditDTO
     * 任务编辑
     */
    public function taskEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof TaskEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $taskId                 = intval($DTOClass->getTaskId());
        $setData                = $RouteConfData->getParamsResData($DTOClass);
        $TaskCtl                = TaskCtl::tag();
        $LogCtl                 = LogCtl::tag();
        $infoSrc                = $TaskCtl->getTaskInfoSrc($taskId);
        if(!isset($infoSrc['task_id'])){
            $logData            = $setData;
            $errMsg             = '任务不存在';
            $LogCtl->logAdd(LogCtl::TASK_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $logData                = ['new' => $setData, 'old' => $infoSrc];
        $updateLotteryVersion   = false;
        if($RouteConfData->isSetParam(TaskEditDTO::_type)){
            $taskData           = [];
            if(!$TaskCtl->getTaskData(intval($infoSrc['task_type']), $setData, $taskData, $updateLotteryVersion)) {
                return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
            }
            $setData['taskData']= CommJson::encodeArray($taskData);
        }
        $result                 = $TaskCtl->taskEdit($taskId, $setData, $updateLotteryVersion);
        if(!$result){
            $errMsg             = '任务编辑失败';
            $LogCtl->logAdd(LogCtl::TASK_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg                 = '任务编辑成功';
        $LogCtl->logAdd(LogCtl::TASK_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}